﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpriteGame.Entities.Actors
{
    class Weapon
    {
        private const float LERP_SPEED = 0.95f;

        protected Int64 coolDown, lastFired;
        protected Vector2 charOffset;
        protected Texture2D texture;
        protected WeaponTypeEnum weaponType;
        protected float maxRecoil, currentRecoilPercent;

        #region Gets

        public Int64 CoolDown
        {
            get { return coolDown; }
        }

        public Int64 LastFired
        {
            get { return lastFired; }
        }

        public Vector2 CharOffset
        {
            get { return charOffset; }
        }

        public float MaxRecoil
        {
            get { return maxRecoil; }
        }

        public float CurrentRecoilPercent
        {
            get { return currentRecoilPercent; }
        }

        public Texture2D Texture
        {
            get { return texture; }
        }

        public WeaponTypeEnum WeaponType
        {
            get { return weaponType; }
        }

        #endregion

        public Weapon(Texture2D _texture, Int32 _coolDown, Vector2 _charOffset, float _maxRecoil, WeaponTypeEnum _weaponType)
        {
            coolDown = _coolDown;
            charOffset = _charOffset;
            maxRecoil = _maxRecoil;

            texture = _texture;
            lastFired = 0;
            currentRecoilPercent = 0;
            weaponType = _weaponType;
        }

        public virtual void Shoot(Int64 _ticks)
        {
            if (lastFired + coolDown <= _ticks)
            {
                //Shoot

                currentRecoilPercent = 100;
            }
        }

        public virtual void Shoot(GameTime _gameTime)
        {
            Shoot(_gameTime.TotalGameTime.Ticks);
        }

        public virtual void Update()
        {
            currentRecoilPercent = MathHelper.Lerp(0, currentRecoilPercent, LERP_SPEED);
        }

        public virtual void Draw(SpriteBatch _spriteBatch, DirectionEnum _direction, Vector2 _charPosition, float _rotation)
        {
            Vector2 recoilOffset = new Vector2(-(float)Math.Sin(_rotation) * (maxRecoil * (currentRecoilPercent / 100)), -(float)Math.Cos(_rotation) * (maxRecoil * (currentRecoilPercent / 100)));

            switch (_direction)
            {
                case DirectionEnum.Forward:
                    {
                        _spriteBatch.Draw(texture, _charPosition + recoilOffset + charOffset, null, Color.White, _rotation, new Vector2(20,20), 1, SpriteEffects.None, 0);
                    }
                    break;
                case DirectionEnum.Backward:
                    {
                        _spriteBatch.Draw(texture, _charPosition - recoilOffset + charOffset, null, Color.White, _rotation, new Vector2(20, 20), 1, SpriteEffects.None, 0);
                    }
                    break;
            }
        }
    }
}
